package com.my.study.controller;

import com.my.study.entity.User;
import com.my.study.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * http://localhost:8379/redis
 * 
 * @author: yidujun
 * @create: 2021/03/15 14:12
 */
@Slf4j
@RestController
@RequestMapping("/redis")
@Api("RedisController")
public class RedisController {

    /**
     * redis中存储的过期时间60s
     */
    private static long ExpireTime = 60;

    @Resource
    private RedisUtil redisUtil;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @GetMapping("/set")
    @ApiOperation(value = "设置值")
    public boolean redisSet(String key){
        User userEntity =new User();
        userEntity.setId(1L);
        userEntity.setGuid(String.valueOf(1));
        userEntity.setName("zhangsan");
        userEntity.setAge(String.valueOf(20));
        userEntity.setCreateTime(new Date());

        return redisUtil.set(key,userEntity,ExpireTime);
    }

    @GetMapping("/get")
    @ApiOperation(value = "根据key从缓存获取值")
    public Object redisget(String key){
        System.out.println(redisUtil.get(key));
        return redisUtil.get(key);
    }

    @GetMapping("/expire")
    @ApiOperation(value = "设置key的过期时间")
    public boolean expire(String key){
        return redisUtil.expire(key, ExpireTime);
    }
    
    @GetMapping("/testRedisTemplate")
    public void testRedisTemplate() {
       
        
        
        
    }
    

}
